package org.university.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.university.common.ApiResponse;
import org.university.entity.Classroom;
import org.university.exception.ResourceNotFoundException;
import org.university.service.ClassroomService;

@CrossOrigin
@RestController
@RequestMapping("/classrooms")
public class ClassroomController {

    private static final Logger LOGGER = LoggerFactory.getLogger(ClassroomController.class);
    private final ClassroomService classroomService;

    public ClassroomController(ClassroomService classroomService) {
        this.classroomService = classroomService;
    }

    /**
     * 获取教室信息分页
     *
     * @param page 页码
     * @param size 页面大小
     * @return 信息分页
     */
    @GetMapping
    public ResponseEntity<ApiResponse<Page<Classroom>>> getClassroom(
            @RequestParam(defaultValue = "0") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        LOGGER.info("查询教室：页码={}, 每页大小={}", page, size);
        Page<Classroom> classrooms = classroomService.findAll(PageRequest.of(page, size));
        return ApiResponse.success(classrooms);
    }

    /**
     * 根据ID查找教室
     */
    @GetMapping("/{id}")
    public ResponseEntity<ApiResponse<Classroom>> getClassroomById(@PathVariable Integer id) {
        LOGGER.info("查询教室ID: {}", id);
        return classroomService.findById(id)
                .map(ApiResponse::success)
                .orElseThrow(() -> new ResourceNotFoundException("未找到id为：" + id + " 的教室"));
    }

    /**
     * 创建教室
     */
    @PostMapping
    public ResponseEntity<ApiResponse<Classroom>> createClassroom(@RequestBody Classroom classroom) {
        LOGGER.info("创建教室ID: {}", classroom.getClassroomId());
        Classroom newClassroom = classroomService.save(classroom);
        return ApiResponse.created(newClassroom);
    }

    /**
     * 更新教室信息
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<Classroom>> updateClassroom(
            @PathVariable Integer id,
            @RequestBody Classroom classroom) {

        LOGGER.info("更新教室ID: {}", id);
        if (classroomService.findById(id).isEmpty()) {
            return ApiResponse.error(HttpStatus.NOT_FOUND, new String("未找到id为：" + id + " 的教室"));
        }
        classroom.setClassroomId(id);
        Classroom updatedClassroom = classroomService.save(classroom);
        return ApiResponse.success(updatedClassroom);
    }

    /**
     * 删除教室
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<ApiResponse<Void>> deleteClassroom(@PathVariable Integer id) {
        LOGGER.info("删除教室ID: {}", id);
        if (classroomService.findById(id).isEmpty()) {
            return ApiResponse.error(HttpStatus.NOT_FOUND, new String("未找到id为：" + id + " 的教室"));
        }
        classroomService.deleteById(id);
        return ResponseEntity.ok().build();
    }
}
